﻿using IronCow;
using Milkify.Interfaces;
using Milkify.Data;
using Milkify.Input;

namespace Milkify.Controllers
{
    public class AccountController : Controller
    {
        IView mView;

        public AccountController(ControllerContext context, Rtm rtm, IViewContainer viewContainer, IViewFactory viewFactory)
            : base(context, rtm, viewContainer)
        {
            mView = viewFactory.Create("Account");
        }

        public override void Execute()
        {
            var viewData = new AccountViewData(new DecoupledCommand(Logout), new DecoupledCommand<string>(NavigateToRTM, CanNavigateToRTM));
            mView.ViewData = viewData;

            ViewContainer.NavigateToView(mView);
        }

        private void Logout()
        {
            Rtm.AuthToken = null;
            Properties.Settings.Default.Token = null;
            Properties.Settings.Default.Save();
            while (ViewContainer.CanNavigateBack)
                ViewContainer.NavigateBack();
        }

        private void NavigateToRTM(string url)
        {
            System.Diagnostics.Process.Start(url);
        }

        private bool CanNavigateToRTM(string url)
        {
            return !string.IsNullOrEmpty(url);
        }
    }
}
